package Module;

import Communication.M2ServerPacket;
import Const.ModuleConst;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class ModuleSender extends Thread {

    private ModuleClient moduleClient;
    private M2ServerPacket greetings;
    private int ID = 0;
    /**
     * Get the value of moduleClient
     *
     * @return the value of moduleClient
     */
    public ModuleClient getModuleClient() {
        return moduleClient;
    }

    /**
     * Set the value of moduleClient
     *
     * @param moduleClient new value of moduleClient
     */
    public void setModuleClient(ModuleClient moduleClient) {
        this.moduleClient = moduleClient;
    }


    /**
    * Default constructor.
    */
    public ModuleSender(ModuleClient moduleClient){
       this.moduleClient = moduleClient;
    }

    @Override
    public void  run()
    {
        try {
              while(moduleClient.isConnected())
                {
                    synchronized(moduleClient.getServerTask()) {

                        int i ;
                        for ( i = 0 ; i < moduleClient.getServerTask().size() ; ++i ){
                            if ( moduleClient.getServerTask().get(i).getTaskID() <=
                                    moduleClient.getLastTaskDoneByServer()){
                                System.out.println("Removing: "+ moduleClient.getServerTask().get(i).getTaskID()
                                        +"\tserv said "+moduleClient.getLastTaskDoneByServer()) ;
                               // moduleClient.getServerTask().remove(moduleClient.getServerTask().get(i));
                                 moduleClient.getServerTask().remove(i);
                                // --i ?;
                            }
                        }


                    greetings = new M2ServerPacket(
                            ModuleConst.TypeLift,
                            ID++,
                            moduleClient.getServerTask());
                    if(greetings.getTaskList().size() > 0 && moduleClient.getTaskDone().size() > 0){

                        greetings.setLastTaskID(moduleClient.getTaskDone().get(ModuleClient.getCurrentTaskNumber()-1).getTaskID());
                        ObjectOutputStream oos = new ObjectOutputStream(moduleClient.getSocket().getOutputStream());
                        oos.writeObject(greetings);
                        oos.flush();

                        System.out.println("sent!tu jest male TODO");
                        // TODO zamienic robienie nowej listy na kasowanie tych taskow, ktore server zrobil - look @pakiet incoming
                        //moduleClient.setNewList();
                        }
                    }

                    Thread.sleep(ModuleConst.TimeDelayLong);
              }
        }catch(IOException ex){
            System.out.println("Blad podczas wysylania");
            System.out.println( "class " + ex.getClass() + " \t " + ex.getMessage() );

            moduleClient.setConnected(false);
            while(!moduleClient.isConnected())
            {
                System.out.print("Trying to reconnect....");
                moduleClient.initSocket();
            }

        }catch(Exception ex){

            System.out.println(ex.getClass() + "\t" + ex.getMessage());
            System.out.println("dupaupa");

        }
        
    }


}//end class
